package com.wuyou.controller;

import com.wuyou.domain.Result;
import com.wuyou.domain.bo.AdvertisingBO;
import com.wuyou.domain.po.Advertising;
import com.wuyou.domain.vo.AdvertisingVO;
import com.wuyou.service.IAdvertisingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘雷雨
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/advertising")
@Api(value = "广告信息接口")
public class AdvertisingController {

    @Autowired
    private IAdvertisingService advertisingService;

    @ApiOperation(value = "添加广告信息")
    @PostMapping("addAdvertising")
    public Result addAdvertising(@RequestBody  AdvertisingVO advertisingVO) {
        try {
            //对象VO转换BO
            AdvertisingBO advertisingBO = new AdvertisingBO();
            BeanUtils.copyProperties(advertisingVO,advertisingBO);
            return Result.OK().setData(advertisingService.addAdvertising(advertisingBO));
        } catch (BeansException e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @ApiOperation(value = "根据主键ID查找一条广告信息")
    @PostMapping("findAdvertisingById")
    public Result findAdvertisingById(@RequestParam Long advertisingId) {
        try {
            return Result.OK().setData(advertisingService.getById(advertisingId));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @ApiOperation(value = "根据订单号修改广告信息")
    @PostMapping("updateAdvertisingByOrderNum")
    public Result updateAdvertisingByOrderNum(@RequestBody Advertising advertising) {
        try {
            return Result.OK().setData(advertisingService.updateAdvertisingByOrderNum(advertising));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @GetMapping("findAdvertisingList")
    public Result findAdvertisingList() {
        try {
            //BO转VO
            List<AdvertisingBO> advertisingBOS = advertisingService.findAdvertisingList();
            List<AdvertisingVO> advertisingVOS = new ArrayList<>();
            for (AdvertisingBO advertisingBO : advertisingBOS) {
                AdvertisingVO advertisingVO = new AdvertisingVO();
                BeanUtils.copyProperties(advertisingBO,advertisingVO);
                advertisingVOS.add(advertisingVO);
            }
            return Result.OK().setData(advertisingVOS);
        } catch (BeansException e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
